<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>岗位权限管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区 -->
    <el-card>
      <!-- 添加按钮 -->
      <el-row>
        <el-col>
          <el-button icon="el-icon-plus" type="primary">添加</el-button>
        </el-col>
      </el-row>
      <!-- 岗位权限列表 -->
      <el-table :data="roleList" border stripe>
        <!-- 展开列 -->
        <el-table-column type="expand">
          <template v-slot="scope">
            <el-row :class="['bdbottom', i1===0?'bdtop':'','vcenter']" v-for="(item1, i1) in scope.row.children" :key="item1.id">
              <!-- 一级权限 -->
              <el-col :span="5">
                <el-tag icon="el-icon-caret-right" closable @close="removeById(scope.row,item1.id)" >{{item1.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 二级 -->
              <el-col :span="19">
                <el-row :class="[i2===0?'':'bdtop','vcenter']" v-for="(item2, i2) in item1.children" :key="item2.id">
                  <el-col :span="6">
                    <el-tag type="success" closable @close="removeById(scope.row,item2.id)" >{{item2.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag v-for="item3 in item2.children" :key="item3.id" type="warning" closable @close="removeById(scope.row,item3.id)" >{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column label="岗位名称" prop="roleName"></el-table-column>
        <el-table-column label="操作" width="300px">
          <template v-slot="scope">
            <!-- <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button> -->
            <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
            <el-button type="warning" icon="el-icon-setting" size="mini" @click="SetDialog(scope.row)">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分配权限 对话框 -->
    <el-dialog title="分配权限" v-model="SetDialogVisible" width="50%" @close="SetDialogClosed">
      <!-- 树形控件 -->
      <el-tree
        :data="rightsList"
        :props="treeProps"
        show-checkbox
        node-key="id"
        default-expand-all
        :default-checked-keys="defKeys"
      ></el-tree>

      <!-- 底部 -->
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="SetDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="SetDialogVisible = false">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      activeRow: "",
      roleList: [],
      rightsList: [],
      SetDialogVisible: false,
      // 树形控件的属性绑定对象
      treeProps: {
        description: "description",
        children: "children"
      },
      // 默认选中的节点id值
      defKeys: []
    };
  },
  mounted() {
    // this.getRoleList();
  },
  methods: {
    // 获取岗位列表
    async getRoleList() {
      // const { data: res } = await this.$http.get("roles");
      // if (res.meta.status !== 200) {
      //   return this.$message.error("获取列表失败");
      // }
      // this.roleList = res.data;
    },
    async removeById(role, rightId) {
      const confirmRes = await this.$confirm("确认删除该权限？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).catch(err => console.log(err));
      if (confirmRes !== "confirm") {
        return this.$message.info("取消删除");
      }
      // const { data: res } = await this.$http.delete(
      //   `roles/${role.id}/rights/${rightId}`
      // );
      // if (res.meta.status !== 200) {
      //   return this.$message.error("删除失败");
      // }
      // role.children = res.data;
    },
    async SetDialog(role) {
      // const { data: res } = await this.$http.get("rights/tree");
      // if (res.meta.status !== 200) {
      //   return this.$message.error("获取权限数据失败");
      // }
      // this.rightsList = res.data;
      // this.getLeafKeys(role,this.defKeys)
      this.SetDialogVisible = true;
    },
    // 通过递归方式，获取岗位下所有三级权限id，保存在defKeys
    getLeafKeys(node,arr){
        if(!node.children){
            return arr.push(node.id)
        }
        node.children.forEach(item => {
            this.getLeafKeys(item,arr);
        });
    },
    SetDialogClosed(){
        this.defKeys=[]
    }
  }
};
</script>

<style lang="less" scoped>
.el-tag {
  margin: 7px;
}
.bdtop {
  border-top: 1px solid #eee;
}
.bdbottom {
  border-bottom: 1px solid #eee;
}
.vcenter {
  display: flex;
  align-items: center;
}
</style>
